
function Machine(config){
    return new StateNode(config);
}
///redux状态和数据混在一起的  xstate状态和数据是分开始的，而且 表示业务流程会非常的清晰
class StateNode{
    constructor(config,machine,value){
        this.config = config;//配置对象
        this.initial = config.initial;//初始状态close
        this.value = value||config.initial;//状态的值 close
        this.machine = machine||this;//存放着状态机节点的根节点 根节点就是自己
        this.context = config.context || this.machine.context;
        this.on = config.on;//遇到什么事件，变成什么状态在on里面定义
        let states = {};
        if(config.states){
            for(let key in config.states)
                states[key] = new StateNode(config.states[key],this.machine,key);
        }
        this.states = states;
    }
    next=(event)=>{
        let {type} = event;//CLICK
        let nextState = this.on[type];//nextState open字符串
        if(typeof nextState === 'string'){
            return this.getStateNode(nextState);//open
        }else{
            let actions = nextState.actions;
            if(Array.isArray(actions)){
                let context = this.context;
                let newContext={};
                actions.forEach(action=>{
                    let assignment= action.assignment;
                    for(let key in assignment){
                        if(typeof assignment[key] === 'function'){
                            newContext[key]=assignment[key](context,event);
                        }else{
                            newContext[key]=assignment[key];
                        }
                    }
                });
                Object.assign(context,newContext);
            }
            return this;
        }
    }
    getStateNode=(stateKey)=>{//open
        return this.machine.states[stateKey];//open对应的状态节点StateNode
    }
}


export default Machine;